﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OpenQuarters.WebQuarters.Core
{
    public partial class Translation : CMSEntity<Translation, long>
    {
        public override System.Linq.Expressions.Expression<Func<Translation, bool>> GetIDSelector(long ID)
        {
            return e => e.TranslationId == ID;
        }

        public override long GetKey(Translation Entity)
        {
            return Entity.TranslationId;
        }

        public static string Translate(string Reference)
        {
            return Translate(Reference, Language.Current.LanguageId);
        }

        public static string Translate(string Reference, string LanguageId)
        {
            var t = Table.FirstOrDefault(e => e.Reference == Reference && e.LanguageId == LanguageId);
            if (t == null)
            {
                t = Table.FirstOrDefault(e => e.Reference == Reference);
            }
            return t != null ? t.Translated : Reference.Replace("_", " ");
        }

        public static Dictionary<string, string> Get(List<string> References)
        {
            var d = new Dictionary<string, string>();
            Table.Where(e => e.LanguageId == Language.Current.LanguageId && References.Contains(e.Reference)).ToList().ForEach(e => d.Add(e.Reference, e.Translated));
            foreach (var r in References)
            {
                if (!d.ContainsKey(r))
                {
                    d.Add(r, r.Replace("_", " "));
                }
            }
            return d;
        }
    }
}
